package org.sja.log2traffic.outputers;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import org.sja.log2traffic.IDateTimeFormatter;
import org.sja.log2traffic.IFileNameProvider;
import org.sja.log2traffic.dataStructures.ConfigurationEntity;
import org.sja.log2traffic.dataStructures.CounterForDateAndResponseTimeEntity;


public class GeneralStatisticToJSON {

	private IDateTimeFormatter dateTimeFormatter;
	private IFileNameProvider fileNameProvider;

	public GeneralStatisticToJSON(IDateTimeFormatter dateTimeFormatter, IFileNameProvider fileNameProvider) {
		this.dateTimeFormatter = dateTimeFormatter;
		this.fileNameProvider = fileNameProvider;
	}

	public void generate(ConfigurationEntity configurationFromTo, CounterForDateAndResponseTimeEntity[] data, String fileNameSuffix) {

		configurationFromTo.getOutputDirectory().mkdirs();
		File fileOutput = new File(configurationFromTo.getOutputDirectory().getAbsolutePath() + '/' + fileNameSuffix);
		PrintWriter writer = null;
		try {
			writer = new PrintWriter(new FileWriter(fileOutput));
			writer.println("{");
			writer.println("from: '" + configurationFromTo.getFromAsFormattedString() + "', ");
			writer.println("to: '" + configurationFromTo.getToAsFormattedString() + "', ");
			writer.println("data: {");
			boolean isFirst = true;
			for (CounterForDateAndResponseTimeEntity cat : data) {
				if (!isFirst){
					writer.println(',');
				}
				writer.print("   '" + cat.getCategory() + "': {");
				writer.print("category: '" + cat.getCategory() + "', ");
				writer.print("count: " + cat.getCount() + ", ");
				writer.print("spentTime: " + cat.getSpendTime() + ", ");
				writer.print("maxRespTime: " + cat.getMaxDateAndTime().getResponseTime() + ", ");
				writer.print("whenMaxRespTime: '" + dateTimeFormatter.longToStringWithMinutes(cat.getMaxDateAndTime().getDate()) + "', ");
				writer.print("trafficFileName: '" + fileNameProvider.provideFileName(configurationFromTo, cat.getCategory(), MultipleAxisChartRenderer.FILE_NAME_SUFFIX) + "'");
				writer.print("}");
				isFirst = false;
			}
			writer.println("}}");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}

	public void generateGeneralInfo(ConfigurationEntity configurationFromTo, String fileName) {

		configurationFromTo.getRootOutputConfigurationDirectory().mkdirs();
		File fileOutput = new File(configurationFromTo.getRootOutputConfigurationDirectory().getAbsolutePath() + '/' + fileName);
		PrintWriter writer = null;
		try {
			writer = new PrintWriter(new FileWriter(fileOutput));
			writer.println("{");
			writer.println("configName: '" + configurationFromTo.getTrafficEnvironment() + "', ");
			writer.println("amountOfHoursOnHoursChart: '" + configurationFromTo.getAmountOfHoursOnHoursChart() + "', ");
			writer.println("amountOfDaysOnDaysChart: '" + configurationFromTo.getAmountOfDaysOnDaysChart() + "', ");
			writer.println("amountOfWeeksOnWeeksChart: '" + configurationFromTo.getAmountOfWeeksOnWeeksChart() + "', ");
			writer.println("amountOfMonthsOnMonthsChart: '" + configurationFromTo.getAmountOfMonthsOnMonthsChart() + "' ");
			writer.println("}");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}

	
}
